﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.DeepProcessing_深加工_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.DeepProcessing_深加工_
{
    public class UpdateEnterpriseHandler : IRequestHandler<UpdateEnterpriseCommand, ApiResult<int>>
    {
        private readonly IBase<EnterpriseModel> enterpriseRepo;
        private readonly IMapper mapper;

        public UpdateEnterpriseHandler(IBase<EnterpriseModel> enterpriseRepo,IMapper mapper)
        {
            this.enterpriseRepo = enterpriseRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateEnterpriseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var enterprise=enterpriseRepo.GetValue(request.EnterpriseId).Result;
            enterprise=mapper.Map(request,enterprise);
            var r=enterpriseRepo.Update(enterprise).Result;
            if(r>0)
            {
                result.Data = r;
                result.Code = ApiEnum.OK;
                result.Msg = "修改深加工企业信心成功";
            }
            else
            {
                result.Data = r;
                result.Code = ApiEnum.Fail;
                result.Msg = "修改深加工企业信心失败";
            }
            return Task.FromResult(result);
        }
    }
}
